import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import base from "@src/store/base";
import { getMemberGiveConfig  } from '@src/api/member';

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor', 'perms'],
  }],
  data: {
    navWChoose: null,
    enableGive:false,
    isShow: null
  },
  observers: {
    'perms,enableGive': function (perms) {
      if ((!perms && this.data.navWChoose !== null) || this.data.isShow !== null) {
        return;
      }
      let navWChoose = null;
      if (perms.MALL){
         navWChoose = 8;
      }else if (perms.BOOKING || perms.BOOKING_HOUR_ROOM || perms.BOOKING_KTV) {
        navWChoose = 7;
      } else if (perms.WCANYIN) {
        navWChoose = 6;
      } else if (perms.MEMBER_BASE&&this.data.enableGive) {
        navWChoose = 666;
      }
      this.setData({navWChoose: navWChoose})
    }
  },
  methods: {
    onLoad(query) {
      const { orderType } = query
      this.$scrollTop = this.selectComponent("#scroll-top");
      getMemberGiveConfig().then(res => {
        let enableGive=res.data&&res.data.enableGive||false
        this.setData({ enableGive})
        query.navWChoose && this.setData({navWChoose: Number(query.navWChoose)})
      })

      if(orderType) {
        const mapSource = {5: 8, 7: 7, 17: 10, 9: 666}
        const isShow = decodeURIComponent(orderType).split(',').filter(item => ~~item)
        this.setData({isShow, navWChoose: mapSource[isShow[0]]})
      }
    },
    onPageScroll(res) {
      this.$scrollTop.onPageScroll(res);
    },
    onReachBottom: function () {
      let child = this.selectComponent(".child")
      child.onReachBottom && child.onReachBottom();
    },
    onPullDownRefresh() {
      let child = this.selectComponent(".child")
      child.onPullDownRefresh && child.onPullDownRefresh();
    },
    handleChange(e) {
      let index = e.currentTarget.dataset.index;
      this.setData({navWChoose: Number(index)})
    },

  }
})
